﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml.Linq;
using System.IO;

namespace gui
{
    public partial class Settings : Form
    {
        ClientSettings settings = new ClientSettings();
        public Settings()
        {
            InitializeComponent();
            this.LoadSettings();
            btnApply.Enabled = false;
            btnCreatDB.Enabled = false;
        }

        private void LoadSettings()
        {
            if (File.Exists(settings.FileName))
            {
                Config xmlConfigFile = new Config();
                xmlConfigFile.GetConfig();
                ipBox.Text = xmlConfigFile.Ip;
                portBox.Text = xmlConfigFile.Port.ToString();
                loginBox.Text = xmlConfigFile.Login;
                dbName.Text = xmlConfigFile.DataBaseName;
                tableName.Text = xmlConfigFile.DataBaseTable;
                compName.Text = xmlConfigFile.ComputerName;

                if (xmlConfigFile.TypeConnection == settings.SocektType)
                {
                    this.radioSocket.Checked = true;
                }

                if (xmlConfigFile.TypeConnection == settings.WCFType)
                {
                    this.radioWCF.Checked = true;
                }
            }
            else
            {
                this.CreateXML(/*ipBox.Text, portBox.Text, loginBox.Text*/);
            }
        }

        private string GetTypeConnection()
        {
            string result = String.Empty;
            if (this.radioSocket.Checked == true)
            {
                result = settings.SocektType;
            }

            if (this.radioWCF.Checked == true)
            {
                result = settings.WCFType;
            }

            return result;
        }

        private void CreateXML(/*string ip, string port, string login*/)
        {
            XElement xml = new XElement(settings.MainElement,
                    new XElement(settings.IpElement, ipBox.Text),
                    new XElement(settings.PortElement, portBox.Text),
                    new XElement(settings.LoginElement, loginBox.Text),
                    new XElement(settings.TypeConnection, this.GetTypeConnection()),
                    new XElement("DataBaseName", dbName.Text),
                    new XElement("DataBaseTable", tableName.Text),
                    new XElement("ComputerName", compName.Text));
            xml.Save(settings.FileName);
        }

        private void CheckInputValues()
        {
            if (ipBox.Text == String.Empty || portBox.Text == String.Empty || loginBox.Text == String.Empty || dbName.Text == String.Empty || tableName.Text == String.Empty || compName.Text == String.Empty)
            {
                MessageBox.Show("Вы заполнили не все поля.", "Ошибка!");
            }
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            this.CheckInputValues();
            if (File.Exists(settings.FileName))
            {
                this.CreateXML(/*ipBox.Text, portBox.Text, loginBox.Text*/);
            }

            this.Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnApply_Click(object sender, EventArgs e)
        {
            if (!File.Exists(settings.FileName))
            {
                this.CreateXML(/*ipBox.Text, portBox.Text, loginBox.Text*/);
            }
            else
            {
                XElement xe;
                xe = XElement.Load(settings.FileName);
                xe.SetElementValue(settings.IpElement, ipBox.Text);
                xe.SetElementValue(settings.PortElement, portBox.Text);
                xe.SetElementValue(settings.LoginElement, loginBox.Text);
                xe.SetElementValue(settings.TypeConnection, this.GetTypeConnection());
                xe.SetAttributeValue("DataBaseName", dbName.Text);
                xe.SetAttributeValue("DataBaseTable", tableName.Text);
                xe.SetAttributeValue("ComputerName", compName.Text);
                xe.Save(settings.FileName);
            }
        }

        private void ipBox_TextChanged(object sender, EventArgs e)
        {
            btnApply.Enabled = true;
        }

        private void portBox_TextChanged(object sender, EventArgs e)
        {
            btnApply.Enabled = true;
        }

        private void loginBox_TextChanged(object sender, EventArgs e)
        {
            btnApply.Enabled = true;
        }

        private void btnCreatDB_Click(object sender, EventArgs e)
        {
            if (dbName.Text == String.Empty || tableName.Text == String.Empty || compName.Text == String.Empty)
            {
                MessageBox.Show("Вы заполнили не все поля.", "Ошибка!");
            }
            else
            {
                DataBase db = new DataBase(dbName.Text, tableName.Text, compName.Text);
                try
                {
                    db.OpenConnection(compName.Text, dbName.Text);
                    /*Console.WriteLine(db.dbExist());
                    if (db.dbExist() == 0)
                    {
                        db.creatDataBase();
                    }*/
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Ошибка");
                }
            }
        }

        private void radioSocket_CheckedChanged(object sender, EventArgs e)
        {
            btnApply.Enabled = true;
        }

        private void radioWCF_CheckedChanged(object sender, EventArgs e)
        {
            btnApply.Enabled = true;
        }

        private void dbName_TextChanged(object sender, EventArgs e)
        {
            btnCreatDB.Enabled = true;
            btnApply.Enabled = true;
        }

        private void tableName_TextChanged(object sender, EventArgs e)
        {
            btnCreatDB.Enabled = true;
            btnApply.Enabled = true;
        }

        private void compName_TextChanged(object sender, EventArgs e)
        {
            btnCreatDB.Enabled = true;
            btnApply.Enabled = true;
        }

    }
}
